# src/models/Currency.py
from . import db
import datetime
from marshmallow import fields, Schema

class CurrencyModel(db.Model):
  """
  Currency Model
  """

  __tablename__ = 'currencies'

  id = db.Column(db.Integer, primary_key=True)
  cyName = db.Column(db.String(20), nullable=False)
  oneDollar = db.Column(db.Float, nullable=False)


  def __init__(self, data):
    self.cyName = data.get('cyName')
    self.oneDollar = data.get('oneDollar')


  def save(self):
    db.session.add(self)
    db.session.commit()

  def update(self, data):
    for key, item in data.items():
      setattr(self, key, item)
    db.session.commit()

  def delete(self):
    db.session.delete(self)
    db.session.commit()
  
  @staticmethod
  def get_all_currencies():
    return CurrencyModel.query.all()

  @staticmethod
  def get_one_currency(id):
    return CurrencyModel.query.get(id)

  def __repr__(self):
    return '<id {}>'.format(self.id)

class CurrencySchema(Schema):
  """
  Currency Schema
  """
  id = fields.Int(dump_only=True)
  cyName = fields.Str(required=True)
  oneDollar = fields.Float(required=True)

